-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPackage.py
executable file
·53 lines (46 loc) · 1.63 KB
/
getPackage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import sys
import re
from subprocess import check_output
installed = str(check_output(["R", "-e installed.packages()[,1]"]))
installed = re.findall('"[A-Za-z0-9\.]*"', installed)
installed = [name.strip('"') for name in installed]
'''installed = []'''
try:
sys.argv[1]
except IndexError:
fname = str(input("Filename: "))
else:
fname = str(sys.argv[1])
rfile = open(fname, 'r')
ftext = rfile.read()
rfile.close()
'''Got the options from
https://github.com/rstudio/packrat/blob/master/R/dependencies.R'''
rDirect = re.findall('[A-Za-z0-9\.]*\s*:{2,3}(?!:)', ftext)
rDirect = [name.strip(':{2,3}') for name in rDirect]
rLibs = re.findall('(?<=library)\s*\(\s*["\'A-Za-z0-9\.]*', ftext)
rReq = re.findall('(?<=require)\s*\(\s*["\'A-Za-z0-9\.]*', ftext)
rName = re.findall('(?<=loadNamespace)\s*\(\s*[\s"\'A-Za-z0-9\.]*', ftext)
rReqname = re.findall('(?<=requireNamespace)\s*\(\s*[\s"\'A-Za-z0-9\.]*', ftext)
rLibs = rLibs + rReq + rName + rReqname + rDirect
rLibs = [re.sub('[\s\(]*', '', name) for name in rLibs]
rLibs = [name.strip("'") for name in rLibs]
rLibs = [name.strip('"') for name in rLibs]
rLibs = list(set(rLibs) - set(installed))
command = '", "'.join(rLibs)
command = command.lstrip('", ')
command = 'install.packages(c("' + command + '"))'
print("Run: \n")
print(command + "\n")
try:
import pyperclip
except ModuleNotFoundError:
print('Cannot copy to clipboard. Run: \n')
print('pip install pyperclip \n')
print('OR \n')
print('conda install -c conda-forge pyperclip \n')
print('to enable this functionality')
else:
pyperclip.copy(command)
print('Command copied to clipboard')