__prog__ ='files2sharepoint.py' __author__ ='Charles Nichols' __created__='06/08/2010' __updated__='' __version__='0.1' __notes__ ='''Upload files to Sharepoint. Requires IronPython and .Net (maybe Mono)''' # =============================================================================> import System, System.Net, System.IO import os, stat, datetime, time # =============================================================================> Base_path = '//some_server/some_share/' Transfer_paths = ['some_sub_dir','another_sub_dir'] # I'll change this later... Base_URL = 'https://some_sharepoint/some_area' # no trailing / Logdate = time.strftime('%Y%m%d_%H%M%S',time.localtime(time.time())) Days = 60 # days to keep logs. Remove_after_upload = 0 # removes files after successful transfer. # <============================================================================= def clean_logs(path,days): cutoff = datetime.timedelta(days) period = datetime.datetime.now() - cutoff for item in os.listdir(path): if 'log' not in item.lower(): continue created = datetime.datetime.fromtimestamp(os.path.getctime(path+'/'+item)) if created < period: os.remove(path+'/'+item) def download(url): # not complete... wc=System.Net.WebClient() wc.Credentials=System.Net.CredentialCache.DefaultCredentials page=wc.DownloadString(url) return page def log(msg,logdate): # I do nothing but write a log file. log_file = logdate+'.log' log_path = Base_path+'/'+log_file f=open(log_path,'w') f.write(str(msg)) f.close def build_paths(base_path,transfer_paths,base_url): # I build path for source (i) to target (sharepoint) transfer_queue = {} for tp in transfer_paths: fp = base_path+'/'+tp # verify full path exists... for item in os.listdir(fp): if 'txt' not in item.lower(): continue source_path = fp+'/'+item target_path = base_url+'/'+item transfer_queue[source_path] = target_path return transfer_queue def upload2sharepoint(host_target,source_file): # (.Net) upload to Sharepoint... # it's that easy! wc = System.Net.WebClient() wc.Credentials=System.Net.CredentialCache.DefaultCredentials bytes = System.IO.File.ReadAllBytes(source_file) wc.UploadData(host_target,'PUT',bytes) # =============================================================================> # MAIN # <============================================================================= error_log = [] try: # build the source/target paths... transfers = build_paths(Base_path,Transfer_paths,Base_URL) # Transfering files to Sharepoint. for file,url in transfers.items(): try: print 'uploading', file, 'to', url upload2sharepoint(url,file)# do upload... error_log.append('%s %s %s success\n' %(time.ctime(),file,url)) # Delete transfered file. if Remove_after_upload: os.remove(file) error_log.append('%s %s removed\n' %(time.ctime(),file)) print 'success' except Exception, error: error_log.append('%s %s failure\n' %(time.ctime(),str(error))) # clean out old logs... clean_logs(Base_path,Days) except Exception, main_err: error_log.append('%s %s failure\n' %(time.ctime(),str(main_err))) # Write the log file. strLogMsg = ''.join(error_log) log(strLogMsg,Logdate) # and on the 7th day, Jobs said "@end"...