I wrote this program to keep my Samba share up to date. The Samba share contains installers for the programs that I use to fix and update computers (ccleaner, mbam, java, .etc). It can also be used for ISO's such as clonezilla.
For the latest version of the script and the accompanying files please go to GitHub.
https://github.com/Gamoholic/KYSU
!!! Important! The only OS I have tested this on is Ubuntu. I will test Windows soon. If it doesn't work on any other OS please let me know! Also, I have no idea how well this works with Python 3. I may test that eventually.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import datetime, os, re, sys, urllib
fh = 'http://www.filehippo.com'
exts = ['.exe', '.msi', '.iso', '.zip']
def make_html(url): return urllib.urlopen(url).read()
def name_generator(name, ver):
if name.endswith('/') or name.find('$') == -1:
final_name = name
else:
final_name = name_replace(name, ver)
while '$' in final_name:
final_name = name_replace(final_name, ver)
if 'filehippo' in name:
a = res('\<a.*?(Latest Version).*?span\>', make_html(name), 0)
b = res('href=\"(.*?)\"', a, 1)
final_name = fh + res('url\=(.*?)\"', make_html(fh + b), 1)
return final_name
def name_replace(name, ver):
name_rep = res('\$[^a-zA-Z]*\$', name, 0)
delim_ver = ver.translate(None, '_.-"')
name_list, name_str, c = [], '', 0
for s in range(len(name_rep)):
if name_rep[s] == '$':
name_list.append(delim_ver[c])
c += 1
else:
name_list.append(name_rep[s])
for s in range(len(name_list)):
name_str += name_list[s]
return name.replace(name_rep, name_str)
def build_dict(filenames):
dict, files = {}, [s for s in filenames for x in exts if s.endswith(x)]
for s in files:
dict[res('(.*)\-', s, 1)] = res('-([\d\.]*)\.\w+', s, 1)
return dict
def res(regex, string, x):
a = re.search(regex, string)
try:
return a.group() if x == 0 else a.group(x)
except AttributeError:
return ''
def main():
print datetime.datetime.now().strftime("%m-%d-%Y")
local_files = os.listdir(sys.argv[2])
big_list = [s.split() for s in open(sys.argv[1], 'rU').read().split('\n')
if s != '' and not s.startswith('#')]
final_list, url_list, final_dict, url_dict = [], [], {}, {}
for list in big_list:
ver = res(list[3], make_html(list[1]), 0)
url, final = name_generator(list[2], ver), name_generator(list[0], ver)
print url
url_list.append(url)
final_list.append(final)
name = res('(.*)\-', final, 1)
url_dict[name], final_dict[name] = url, final
local_dict, update_dict = build_dict(local_files), build_dict(final_list)
c_up, c_new = 0,0
for key in update_dict:
down_loc = sys.argv[2] + final_dict[key]
if key in local_dict:
if local_dict[key] != update_dict[key]:
c_up += 1
print key
del_var = key + '-' + local_dict[key]
for s in local_files:
if s.find(del_var) != -1:
print ' Deleted', s
os.remove(sys.argv[2] + s)
print ' Downloading', final_dict[key]
urllib.urlretrieve(url_dict[key], down_loc)
else:
c_new += 1
print key
print ' Downloading for first time!', '\n', ' Downloading', final_dict[key]
urllib.urlretrieve(url_dict[key], down_loc)
print
if c_up == 1:
print '1 file updated.'
else:
print c_up, 'files updated.'
if c_new == 1:
print '1 new file.'
else:
print c_new, 'new files.'
print '', '\n'
main()
|
Revision 2: 1) Fixed a bug where having improperly named files would cause a crash. 2) Cleaned up the code, reducing the number of lines from 124 to 87. As a result, it might be harder to read. If you have a hard time reading the code check out Revision 1 first.
Revision 3: 1) Changed the name to KYSU. 2) Created a GitHub page. 3) Tweaked the output formatting. 4) Various other small tweaks here and there.
Revision 4: I will not update this page very often any more. Please go to GitHub to download this project.
Revision 5: Various changes. Biggest is the change from a Tab Width of 2 to 4. Also, changed the license to GPL 3.