def current_branch(path): """ Returns. git repository source url, TYPE: str """ pwd = os.getcwd() os.chdir(path) try: if '.git' in os.listdir('.'): branch = subprocess.getoutput('git branch').split('*')[1].split( '\n')[0][1:] else: ex = Exception( '%s: Unable to identify current branch - path not a git repository: %s' % (inspect.stack()[0][3], path)) raise ex os.chdir(pwd) # return cursor except IndexError: logger.exception('%s: problem retrieving git branch for %s' % (inspect.stack()[0][3], path)) return '' return branch
def translate_1_2(): """ :param sl: source language :type sl: string :param tl: target language :type tl: string :param m: mode ( 1 for normal, 2 for better ) :type m: int :param t: text to be translated :type t: string Translates given text. """ keys = ('t', 'm', 'sl', 'tl') text, mode, source, target = map(lambda k: request.form[k].strip(), keys) try: payload = translate(text, mode, source, target, 't') return jsonify(payload) except HTTPException as e: return e.message, e.status_code except Exception as e: logger.exception(e) return str(e), 500
def tar_archive(archive, source_dir): """ Summary. - Creates .tar.gz compressed archive - Checks that file was created before exit Returns: Success | Failure, TYPE: bool """ try: with tarfile.open(archive, "w:gz") as tar: tar.add(source_dir, arcname=os.path.basename(source_dir)) if os.path.exists(archive): return True except OSError: logger.exception('{}: Unable to create tar archive {}'.format( inspect.stack()[0][3], archive)) except Exception as e: logger.exception( '%s: Unknown problem while creating tar archive %s:\n%s' % (inspect.stack()[0][3], archive, str(e))) return False
def ospackages(pkg_list): """Summary Install OS Package Prerequisites Returns: Success | Failure, TYPE: bool """ try: for pkg in pkg_list: if is_installed(pkg): logger.info(f'{pkg} binary is already installed - skip') continue elif which('yum'): cmd = 'sudo yum install ' + pkg + ' 2>/dev/null' print(subprocess.getoutput(cmd)) elif which('dnf'): cmd = 'sudo dnf install ' + pkg + ' 2>/dev/null' print(subprocess.getoutput(cmd)) else: logger.warning( '%s: Dependent OS binaries not installed - package manager not identified' % inspect.stack()[0][3]) except OSError as e: logger.exception('{}: Problem installing os package {}'.format( inspect.stack()[0][3], pkg)) return False return True
def convert_mash_to_zeromq_message(mash): try: logger.debug('mash: {0}'.format(mash)) obj = [{'id':mash.id, 'key':mash.key, 'song1':mash.song1, 'song2':mash.song2, 'status':mash.status}] return json.dumps(obj) except Exception, err: logger.exception('Something bad happened: convert_mash_to_zeromq_message, mash={0}'.format(mash))
def tresponse_rate(tresponse_id): rv = int(request.form['r']) if not (rv == -1 or rv == 1): return 'Invalid rating\n', 400 tresponse = TranslationResponse.fetch(id_b62=tresponse_id) if tresponse == None: return 'Requested resource does not exist\n', 404 r = Rating.query.filter_by(translation_id=tresponse.id, user_id=current_user.id).first() if r == None: r = Rating.insert( commit=False, translation_id=tresponse.id, user_id=current_user.id, rating=rv ) else: r.timestamp = datetime.now(tz=pytz.utc) r.rating = rv try: db.session.commit() return jsonify(r.serialize()) except Exception as e: logger.exception(e) db.session.rollback() return str(e), 500
def create_builddirectory(path, version, force): """ Summary: - Creates the deb package binary working directory - Checks if build artifacts preexist; if so, halts - If force is True, continues even if artifacts exist (overwrites) Returns: Success | Failure, TYPE: bool """ try: builddir = PROJECT + '-' + version + '_amd64' # rm builddir when force if exists if force is True and builddir in os.listdir(path): rmtree(path + '/' + builddir) elif force is False and builddir in os.listdir(path): stdout_message( 'Cannot create build directory {} - preexists. Use --force option to overwrite' .format(builddir), prefix='WARN', severity='WARNING') return None # create build directory os.mkdir(path + '/' + builddir) except OSError as e: logger.exception('{}: Unable to create build directory {}'.format( inspect.stack()[0][3], builddir)) return builddir
def mash(key): try: logger.debug('/mash/{0}'.format(key)) mash = rdb_client.get('mashes', key) mash_model = MashMessage(mash['id'], mash['key'], mash['song1'], mash['song2'], mash['status']) return render_template('mash.html', mash=mash_model) except Exception, err: logger.exception('Something bad happened: mash, key={0}'.format(key))
def resubmit(key): try: mash = rdb_client.get('mashes', key) mash_model = MashMessage(mash['id'], mash['key'], mash['song1'], mash['song2'], mash['status']) logger.debug('resubmit mash:{0}'.format(mash_model)) socket.send_json(convert_mash_to_zeromq_message(mash_model)) return redirect(url_for('mash', key=mash_model.key)) except Exception, err: logger.exception('Something bad happened: resubmit, key={0}'.format(key))
def translation_response_delete(response_id): tres = TranslationResponse.fetch(response_id) try: tres.delete(current_user) return jsonify(dict(request_id=uuid_to_b62(tres.request_id))) except Exception as e: logger.exception(e) return str(e), 500
def save_file(file, key): try: if file and allowed_file(file.filename): filename = secure_filename(file.filename) folder = os.path.join(app.root_path, cfg.UPLOAD_FOLDER, key) if not os.path.exists(folder): os.makedirs(folder) file.save(os.path.join(folder, filename)) return filename except Exception, err: logger.exception('Something bad happened: save_file, key={0}, filename={1}'.format(key, file.filename))
def preclean(dir): """ Cleans residual build artifacts """ try: if os.path.exists(dir): rmtree(dir) except OSError as e: logger.exception( '%s: Error while cleaning residual build artifacts: %s' % (inspect.stack()[0][3], str(e))) return False return True
def list(page = 1): try: logger.debug('/list/{0}'.format(page)) count = rdb_client.count('mashes', {}) mashes = rdb_client.filter_paging('mashes', {}, PER_PAGE, page) mash_list = [] for mash in mashes: mash_list.append(MashMessage(mash['id'], mash['key'], mash['song1'], mash['song2'], mash['status'])) logger.debug('mash:{0}'.format(mash)) if not mashes and page != 1: abort(404) pagination = Pagination(page, PER_PAGE, count) return render_template('list.html', pagination=pagination, mashes=mash_list) except Exception, err: logger.exception('Something bad happened: list')
def home(): if request.method == 'POST': try: key = uuid.uuid4().hex song1 = request.files['song1'] song1Filename = save_file(song1, key) song2 = request.files['song2'] song2Filename = save_file(song2, key) status = 'uploaded' mash = MashMessage(key, key, song1Filename, song2Filename, status) rdb_client.insert('mashes', {'id':key, 'key': key, 'song1': song1Filename, 'song2': song2Filename, 'status': status}) logger.debug('new mash:{0}'.format(mash)) socket.send_json(convert_mash_to_zeromq_message(mash)) return redirect(url_for('mash', key=mash.key)) except Exception, err: logger.exception('Something bad happened:')
def translation_request_response_api(request_id): """Translation response API""" def post(request_id): translated_text = request.form['text'].strip() if len(translated_text) <= 0: return _('Please provide a non-empty translation.'), 400 else: tresp = TranslationResponse.insert( request_id=treq.id, user_id=current_user.id, source=treq.source, target=treq.target, mode=3, original_text_hash=treq.original_text_hash, translated_text=translated_text, ) # TODO: Refactor the following code. This should be handled in the model class. # More precisely, in the insert() function. # All users who are watching the translation request watching = Watching.query.filter_by(entity_type='TranslationRequest', entity_id=treq.id) # Put them in the queue to get notified for w in watching: NotificationQueue.insert(user_id=w.user_id, payload=uuid_to_b62(treq.id)) payload = tresp.serialize() payload['message'] = _('Your translation has been posted.') return jsonify(payload) treq = TranslationRequest.fetch(id_b62=request_id) if treq == None: return _('Requrested resource does not exist'), 404 dispatch = dict( post=post) try: return dispatch[request.method.lower()](request_id) except Exception as e: logger.exception(e) return str(e), 500
def watch(): def get(): """Query watching state.""" entity_id = b62_to_uuid(request.args['entity_id']) entity_type = request.args['entity_type'] watching = Watching.query.filter_by(user_id=current_user.id, entity_type=entity_type, entity_id=str(entity_id)).first() if watching == None: return '' else: return jsonify(watching.serialize()) def post(): """Toggle watching state.""" entity_id = b62_to_uuid(request.form['entity_id']) entity_type = request.form['entity_type'] watching = Watching.query.filter_by(user_id=current_user.id, entity_type=entity_type, entity_id=str(entity_id)).first() if watching == None: watching = Watching.insert(user_id=current_user.id, entity_type=entity_type, entity_id=str(entity_id)) return jsonify(watching.serialize()) else: db.session.delete(watching) db.session.commit() return '' dispatch = dict( get=get, post=post) try: return dispatch[request.method.lower()]() except Exception as e: db.session.rollback() logger.exception(e) return str(e), 500
def postbuild(version, version_module, builddir_path, debian_root): """ Summary. Post-build clean up Returns: Success | Failure, TYPE: bool """ root = git_root() project_dirname = root.split('/')[-1] build_root = os.path.split(builddir_path)[0] package = locate_deb(build_root) try: if package: copyfile(package, debian_root) package_path = debian_root + '/' + os.path.split(package)[1] # remove build directory, residual artifacts if os.path.exists(builddir_path): rmtree(builddir_path) if os.path.exists(root + '/scripts/' + version_module): os.remove(root + '/scripts/' + version_module) # rewrite version file with current build version with open(root + '/core/' + version_module, 'w') as f3: f2 = ['__version__=\"' + version + '\"\n'] f3.writelines(f2) path = project_dirname + (root + '/core/' + version_module)[len(root):] stdout_message('{}: Module {} successfully updated.'.format( inspect.stack()[0][3], yl + path + rst)) if display_package_contents(BUILD_ROOT, VERSION): return package_path except OSError as e: logger.exception('{}: Postbuild clean up failure: {}'.format( inspect.stack()[0][3], e)) return False return package_path
def flush_notification_queue(): """I know this is a pretty silly solution, but for some reason gettext() does not work when I invoke it from notification.py that runs on a shell.""" def sendmail(notification): url = url_for('translation_responses', request_id=notification.payload, _external=True) body = _('{0},\n\nSomeone has posted a translation. Check out at {1}').format(notification.user.name, url) message = Message( subject=_('You have a notification from Better Translator'), body=body, sender=(_('app-title'), '*****@*****.**'), recipients=[notification.user.email] ) mail.send(message) db.session.delete(notification) from flask.ext.mail import Mail, Message app.config['MAIL_SERVER'] = config.MAIL_SERVER app.config['MAIL_PORT'] = config.MAIL_PORT app.config['MAIL_USERNAME'] = config.MAIL_USERNAME app.config['MAIL_PASSWORD'] = config.MAIL_PASSWORD mail = Mail(app) try: for notification in NotificationQueue.query.limit(100): sendmail(notification) db.session.commit() return '' except Exception as e: logger.exception(e) return str(e), 500
def build_package(build_root, builddir): """ Summary. Creates final os installable package for current build, build version Returns: Success | Failure, TYPE: bool """ try: pwd = os.getcwd() os.chdir(build_root) if os.path.exists(builddir): cmd = 'dpkg-deb --build ' + builddir + ' 2>/dev/null' stdout_message('Building {}... '.format(bn + builddir + rst)) stdout_message(subprocess.getoutput(cmd)) os.chdir(pwd) else: logger.warning( 'Build directory {} not found. Failed to create .deb package'. format(builddir)) os.chdir(pwd) return False except OSError as e: logger.exception('{}: Error during os package creation: {}'.format( inspect.stack()[0][3], e)) return False except Exception as e: logger.exception( '{}: Unknown Error during os package creation: {}'.format( inspect.stack()[0][3], e)) return False return True
def tresponse_post_facebook(tresponse_id): """ Post a link on the current_user's Facebook timeline https://developers.facebook.com/docs/reference/api/privacy-parameter/ """ tresp = TranslationResponse.fetch(id_b62=tresponse_id) target_language = _(VALID_LANGUAGES[tresp.request.target]) try: graph = facebook.GraphAPI(session.get('oauth_token')[0]) #graph.put_object('me', 'feed', message='This is a test with a <a href="http://translator.suminb.com">link</a>') post = graph.put_wall_post('', dict( name=_('app-title').encode('utf-8'), link='http://translator.suminb.com/trequest/{}/responses'.format(uuid_to_b62(tresp.request_id)), caption=_('{} has completed a translation challenge').format(tresp.user.name).encode('utf-8'), description=_('How do you say "{0}" in {1}?').format(tresp.request.original_text, target_language).encode('utf-8'), picture='http://translator.suminb.com/static/icon_128.png', #privacy={'value':'SELF'} )) post_log = TranslationPostLog.insert( request_id=tresp.request_id, user_id=current_user.id, target='Facebook', post_id=post['id'], ) return jsonify(dict( post_id=post['id'], message=_('Your translation has been posted on your timeline.') )) except Exception as e: logger.exception(e) return str(e), 500
def translate(text, mode, source, target, client='x'): if len(text) == 0: raise HTTPException('Text cannot be empty.', 400) if len(text) > MAX_TEXT_LENGTH: raise HTTPException('Text too long.', 413) if source == target: return dict( id=None, id_b62=None, intermediate_text=None, translated_text=text) if source not in VALID_LANGUAGES.keys(): raise HTTPException('Invalid source language.', 400) if target not in VALID_LANGUAGES.keys(): raise HTTPException('Invalid target language.', 400) original_text_hash = nilsimsa.Nilsimsa(text.encode('utf-8')).hexdigest() user_agent = request.headers.get('User-Agent') access_log = TranslationAccessLog.insert( commit=False, user_id=current_user.id if not current_user.is_anonymous() else None, user_agent=user_agent, remote_address=get_remote_address(request), ) treq = TranslationRequest.fetch( original_text_hash=original_text_hash, source=source, target=target) if treq == None: treq = TranslationRequest.insert( commit=False, user_id=None, source=source, target=target, original_text=text, original_text_hash=original_text_hash, ) tresp = TranslationResponse.fetch( original_text_hash=original_text_hash, source=source, target=target, mode=mode) if tresp == None: translated_raw = None translated_text = None intermediate_raw = None intermediate_text = None # NOTE: The following may be time consuming operations # FIXME: Refactor this code. Looks crappy. if mode == '1': if client == 't': translated_raw = __translate__(text, source, target, client, user_agent) translated_text = ' '.join(map(lambda x: x[0], translated_raw[0])) else: translated_text = __translate__(text, source, target, client, user_agent) elif mode == '2': if client == 't': intermediate_raw = __translate__(text, source, 'ja', client, user_agent) intermediate_text = ' '.join(map(lambda x: x[0], intermediate_raw[0])) translated_raw = __translate__(intermediate_text, 'ja', target, client, user_agent) translated_text = ' '.join(map(lambda x: x[0], translated_raw[0])) else: intermediate_text = __translate__(text, source, 'ja', client, user_agent) translated_text = __translate__(intermediate_text, 'ja', target, client, user_agent) else: return HTTPException('Invalid translation mode.', 400) tresp = TranslationResponse.insert( commit=False, request_id=treq.id, source=source, target=target, mode=mode, original_text_hash=original_text_hash, intermediate_text=intermediate_text, intermediate_raw=intermediate_raw, translated_text=translated_text, translated_raw=translated_raw, ) try: db.session.commit() except Exception as e: logger.exception(e) db.session.rollback() return dict( id=base62.encode(uuid.UUID(tresp.id).int), request_id=base62.encode(uuid.UUID(treq.id).int), intermediate_text=tresp.intermediate_text, intermediate_raw=tresp.intermediate_raw, translated_text=tresp.translated_text, translated_raw=tresp.translated_raw, )
def __translate__(text, source, target, client='x', user_agent=DEFAULT_USER_AGENT): """ text: text to be translated source: source language target: target language """ if source == target: return text from hallucination import ProxyFactory proxy_factory = ProxyFactory( db_engine=db.engine, logger=logger ) if not re.match(r'Mozilla/\d+\.\d+ \(.*', user_agent): user_agent = 'Mozilla/5.0 (%s)' % user_agent headers = { 'Referer': 'http://translate.google.com', 'User-Agent': user_agent, 'Content-Length': str(sys.getsizeof(text)) } payload = { 'client': client, 'sl': source, 'tl': target, 'text': text, } url = 'http://translate.google.com/translate_a/t' req = None try: req = proxy_factory.make_request(url, headers=headers, params=payload, req_type=requests.post, timeout=2, pool_size=10) except Exception as e: logger.exception(e) if req == None: # if request via proxy fails req = requests.post(url, headers=headers, data=payload) if req.status_code != 200: raise HTTPException( ('Google Translate returned HTTP {}'.format(req.status_code)), req.status_code) if client == 'x': data = json.loads(req.text) try: #if target == 'ja': # sentences = data['sentences'] sentences = data['sentences'] except: sentences = data['results'][0]['sentences'] result = ' '.join(map(lambda x: x['trans'], sentences)) # Remove unneccessary white spaces return '\n'.join(map(lambda x: x.strip(), result.split('\n'))) elif client == 't': return parse_javascript(req.text) else: raise Exception("Unsupported client '{}'".format(client))
def builddir_content_updates(param_dict, osimage, version): """ Summary. Updates builddir contents: - main exectuable has path to libraries updated - builddir DEBIAN/control file version is updated to current - updates the version.py file if version != to __version__ contained in the file. This occurs if user invokes the -S / --set-version option Args: :root (str): project root full fs path :builddir (str): dirname of the current build directory :binary (str): name of the main exectuable :version (str): version label provided with --set-version parameter. None otherwise Returns: Success | Failure, TYPE: bool """ root = git_root() project_dirname = root.split('/')[-1] build_root = TMPDIR debian_dir = 'DEBIAN' control_filename = param_dict['ControlFile']['Name'] deb_src = root + '/packaging/deb' major = '.'.join(version.split('.')[:2]) minor = version.split('.')[-1] # files binary = param_dict['Executable'] builddir = param_dict['ControlFile']['BuildDirName'] version_module = param_dict['VersionModule'] dockeruser = param_dict['DockerUser'] issues_url = param_dict['IssuesUrl'] project_url = param_dict['ProjectUrl'] buildarch = param_dict['ControlFile']['BuildArch'] # full paths builddir_path = build_root + '/' + builddir debian_path = builddir_path + '/' + debian_dir control_filepath = debian_path + '/' + control_filename binary_path = builddir_path + '/usr/local/bin' lib_src = root + '/' + 'core' lib_dst = builddir_path + '/usr/local/lib/' + PROJECT # assemble dependencies deplist = None for dep in param_dict['DependencyList']: if deplist is None: deplist = str(dep) else: deplist = deplist + ', ' + str(dep) try: # main exec bin: update pkg_lib path, LOG_DIR with open(binary_path + '/' + binary) as f1: f2 = f1.readlines() for index, line in enumerate(f2): if line.startswith('pkg_lib='): newline = 'pkg_lib=' + '\"' + '/usr/local/lib/' + PROJECT + '\"\n' f2[index] = newline elif line.startswith('LOG_DIR='): logline = 'LOG_DIR=' + '\"' + '/var/log' + '\"\n' f2[index] = logline f1.close() # rewrite bin with open(binary_path + '/' + binary, 'w') as f3: f3.writelines(f2) path = project_dirname + (binary_path + '/' + binary)[len(root):] stdout_message('Bin {} successfully updated.'.format(yl + path + rst)) # debian control files with open(control_filepath) as f1: f2 = f1.readlines() for index, line in enumerate(f2): if line.startswith('Version:'): newline = 'Version: ' + version + '\n' f2[index] = newline f1.close() # rewrite file with open(control_filepath, 'w') as f3: f3.writelines(f2) path = project_dirname + (control_filepath)[len(root):] stdout_message( 'Control file {} version updated.'.format(yl + path + rst)) ## rewrite version file with current build version in case delta ## # orig source version module with open(lib_src + '/' + version_module, 'w') as f3: f2 = ['__version__=\"' + version + '\"\n'] f3.writelines(f2) path = project_dirname + (lib_src + '/' + version_module)[len(root):] stdout_message('Module {} successfully updated.'.format(yl + path + rst)) # package version module with open(lib_dst + '/' + version_module, 'w') as f3: f2 = ['__version__=\"' + version + '\"\n'] f3.writelines(f2) path = project_dirname + (lib_dst + '/' + version_module)[len(root):] stdout_message('Module {} successfully updated.'.format(yl + path + rst)) ## Debian control file content updates ## if os.path.exists(control_filepath): # update specfile - major version for line in fileinput.input([control_filepath], inplace=True): print(line.replace('ISSUES_URL', issues_url), end='') stdout_message(f'Updated {control_filepath} with ISSUES_URL', prefix='OK') # update specfile - minor version for line in fileinput.input([control_filepath], inplace=True): print(line.replace('DEPLIST', deplist), end='') stdout_message('Updated {} with dependcies ({})'.format( yl + control_filepath + rst, deplist), prefix='OK') # update specfile - Dependencies for line in fileinput.input([control_filepath], inplace=True): print(line.replace('PROJECT_URL', project_url), end='') stdout_message('Updated {} with project url ({})'.format( yl + control_filepath + rst, project_url), prefix='OK') # update specfile - Dependencies for line in fileinput.input([control_filepath], inplace=True): print(line.replace('BUILD_ARCH', buildarch), end='') stdout_message('Updated {} with arch ({})'.format( yl + control_filepath + rst, buildarch), prefix='OK') except OSError as e: logger.exception('%s: Problem while updating builddir contents: %s' % (inspect.stack()[0][3], str(e))) return False return True
def builddir_structure(param_dict, version): """ Summary. - Updates paths in binary exectuable - Updates Args: :root (str): full path to root directory of the git project :builddir (str): name of current build directory which we need to populate Vars: :lib_path (str): src path to library modules in project root :builddir_path (str): dst path to root of the current build directory (/<path>/nlines-1.X.X dir) Returns: Success | Failure, TYPE: bool """ root = git_root() project_dirname = root.split('/')[-1] core_dir = root + '/' + 'core' config_dir = root + '/' + 'config' build_root = TMPDIR # files binary = param_dict['Executable'] control_file = param_dict['ControlFile']['Name'] compfile = param_dict['BashCompletion'] builddir = param_dict['ControlFile']['BuildDirName'] # full paths builddir_path = build_root + '/' + builddir deb_src = root + '/packaging/deb' debian_dir = 'DEBIAN' debian_path = deb_src + '/' + debian_dir binary_path = builddir_path + '/usr/local/bin' lib_path = builddir_path + '/usr/local/lib/' + PROJECT comp_src = root + '/' + 'bash' comp_dst = builddir_path + '/etc/bash_completion.d' arrow = yl + Colors.BOLD + '-->' + rst try: stdout_message( f'Assembling build directory artifacts in {bn + builddir + rst}') # create build directory if os.path.exists(builddir_path): rmtree(builddir_path) os.makedirs(builddir_path) stdout_message( message='Created builddir_path: {}'.format(yl + builddir_path + rst), prefix='OK') if not os.path.exists(builddir_path + '/' + debian_dir): copytree(debian_path, builddir_path + '/' + debian_dir) # status msg _src_path = '../' + project_dirname + debian_path.split( project_dirname)[1] _dst_path = builddir_path + '/' + debian_dir stdout_message(message='Copied: {} {} {}'.format( lk + _src_path + rst, arrow, lk + _dst_path + rst), prefix='OK') if not os.path.exists(binary_path): os.makedirs(binary_path) _dst_path = binary_path stdout_message(message='Created: {}'.format(lk + _dst_path + rst), prefix='OK') if not os.path.exists(binary_path + '/' + PROJECT): binary_src = PROJECT_ROOT + '/' + binary binary_dst = binary_path + '/' + binary copyfile(binary_src, binary_dst) # status msg _src_path = '../' + project_dirname + '/' + os.path.split( binary_src)[1] _dst_path = '../' + project_dirname + '/' + os.path.split( binary_dst)[1] stdout_message(message='Copied:\t{} {} {}'.format( lk + _src_path + rst, arrow, lk + _dst_path + rst), prefix='OK') if not os.path.exists(lib_path): os.makedirs(lib_path) # create library dir in builddir # status msg branching _dst_path = '../' + project_dirname + lib_path.split( project_dirname)[1] if os.path.exists(lib_path): stdout_message(message='Created:\t{}'.format(lk + _dst_path + rst), prefix='OK') else: stdout_message( message='Failed to create:\t{}'.format(lk + _dst_path + rst), prefix='FAIL') for libfile in os.listdir(core_dir): if os.path.exists(lib_path + '/' + libfile): stdout_message( f'{libfile} target exists - skip adding to builddir') if libfile.endswith('.log'): # log file, do not place in build logger.info(f'{libfile} is log file - skip adding to builddir') else: # place lib files in build lib_src = core_dir + '/' + libfile lib_dst = lib_path + '/' + libfile copyfile(lib_src, lib_dst) # status msg _src_path = '../' + project_dirname + lib_src.split( project_dirname)[1] _dst_path = '../' + project_dirname + lib_dst.split( project_dirname)[1] stdout_message(message='Copied:\t{} {} {}'.format( lk + _src_path + rst, arrow, lk + _dst_path + rst), prefix='OK') for confile in os.listdir(config_dir): if not os.path.exists(lib_path + '/config'): os.makedirs(lib_path + '/config') # create config dir in builddir _src = config_dir + '/' + confile _dst = lib_path + '/config/' + confile copyfile(_src, _dst) # status msg _src_path = '../' + project_dirname + _src.split( project_dirname)[1] _dst_path = '../' + project_dirname + _dst.split( project_dirname)[1] stdout_message(message='Copied:\t{} {} {}'.format( lk + _src_path + rst, arrow, lk + _dst_path + rst), prefix='OK') if not os.path.exists(comp_dst): # create path os.makedirs(comp_dst) _dst_path = '../' + project_dirname + comp_dst.split( project_dirname)[1] stdout_message(message='Created:\t{}'.format(lk + _dst_path + rst), prefix='OK') # copy for artifact in list( filter(lambda x: x.endswith('.bash'), os.listdir(comp_src))): copyfile(comp_src + '/' + artifact, comp_dst + '/' + artifact) except OSError as e: logger.exception('{}: Problem creating dirs on local fs'.format( inspect.stack()[0][3])) return False return True
def facebook_authorized(resp): if resp is None: return 'Access denied: reason=%s error=%s' % ( request.args['error_reason'], request.args['error_description'] ), 401 session['oauth_token'] = (resp['access_token'], '') # Fields to fetch # https://developers.facebook.com/docs/reference/api/user/ fields = ('id', 'name', 'first_name', 'last_name', 'middle_name', 'username', 'gender', 'locale', 'picture', 'link', 'age_range', 'timezone', 'updated_time', 'verified', 'bio', 'birthday', 'email', 'location', 'website', 'work') me = facebook_app.get('/me', data=dict(fields=','.join(fields))) print me.data # Somehow this not only is disfunctional, but also it prevents other # session values to be set #session['oauth_data'] = me.data key_mappings = { # User model : Facebook OAuth 'oauth_id': 'id', 'oauth_username': '******', 'given_name': 'first_name', 'family_name': 'last_name', 'email': 'email', 'locale': 'locale', 'gender': 'gender', } try: user = User.query.filter_by(oauth_id=me.data['id']).first() if user == None: payload = dict(extra_info=json.dumps(me.data)) for key in key_mappings: oauth_key = key_mappings[key] payload[key] = me.data[oauth_key] user = User.insert(**payload) else: key_mappings.pop('oauth_id') for key in key_mappings: oauth_key = key_mappings[key] setattr(user, key, me.data[oauth_key]) user.extra_info = json.dumps(me.data) db.session.commit() # except IntegrityError as e: # logger.info('User %s (%s) already exists.' % (payload['oauth_username'], # payload['oauth_id'])) except Exception as e: logger.exception(e) return str(e), 500 login_user(user) keys = ('id', 'username', 'first_name', 'last_name', 'email', 'locale', 'gender',) for key in keys: session['oauth_%s' % key] = me.data[key] return redirect(request.args.get('next', '/'))
def translate_1_0(): """ :param sl: source language :type sl: string :param tl: target language :type tl: string :param m: mode ( 1 for normal, 2 for better ) :type m: int :param t: text to be translated :type t: string Translates given text. **Example Request**: .. sourcecode:: http POST /v1.0/translate HTTP/1.1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.99 Safari/537.22 Host: 192.168.0.185:5000 Accept: */* Content-Length: 57 Content-Type: application/x-www-form-urlencoded sl=ko&tl=en&m=2&t=여러분이 몰랐던 구글 번역기 **Example Response** .. sourcecode:: http HTTP/1.0 200 OK Content-Type: application/json Content-Length: 90 Server: Werkzeug/0.8.3 Python/2.7.3 Date: Wed, 10 Apr 2013 06:43:13 GMT { "translated_text": "Google translation that you did not know", "serial_b62": "0z19x", "intermediate_text": "\u7686\u3055\u3093\u304c\u77e5\u3089\u306a\u304b\u3063\u305fGoogle\u306e\u7ffb\u8a33" } **Example iOS Code using ILHTTPClient** ILHTTPClient: https://github.com/isaaclimdc/ILHTTPClient .. sourcecode:: objective-c ILHTTPClient *client = [ILHTTPClient clientWithBaseURL:@"http://translator.suminb.com/" showingHUDInView:self.view]; NSDictionary *params = @{ @"sl": @"en", @"tl": @"ko", @"m": @"2", @"t": @"Google translation that you did not know." }; [client postPath:@"/v1.0/translate" parameters:params loadingText:@"Loading..." successText:@"Success!" multiPartForm:^(id<AFMultipartFormData> formData) { } success:^(AFHTTPRequestOperation *operation, NSString *response) { NSLog(@"%@", response); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { } ]; """ keys = ('t', 'm', 'sl', 'tl') text, mode, source, target = map(lambda k: request.form[k].strip(), keys) try: return jsonify(translate(text, mode, source, target)) except HTTPException as e: return e.message, e.status_code except Exception as e: logger.exception(e) return str(e), 500
def prebuild(builddir, volmnt, parameter_file): """ Summary. Prerequisites and dependencies for build execution """ def preclean(dir): """ Cleans residual build artifacts """ try: if os.path.exists(dir): rmtree(dir) except OSError as e: logger.exception( '%s: Error while cleaning residual build artifacts: %s' % (inspect.stack()[0][3], str(e))) return False return True version_module = json.loads(read(parameter_file))['VersionModule'] if preclean(builddir) and preclean(volmnt): stdout_message( f'Removed pre-existing build artifacts ({builddir}, {volmnt})') os.makedirs(builddir) os.makedirs(volmnt) root = git_root() lib_relpath = 'core' lib_path = root + '/' + lib_relpath sources = [lib_path] illegal = ['__pycache__'] module = inspect.stack()[0][3] try: global __version__ sys.path.insert(0, os.path.abspath(git_root() + '/' + lib_relpath)) from version import __version__ # normalize path sys.path.pop(0) except ImportError as e: logger.exception( message='Problem importing program version module (%s). Error: %s' % (__file__, str(e)), prefix='WARN') except Exception as e: logger.exception('{}: Failure to import version module'.format( inspect.stack()[0][3])) return False ## clean up source ## try: for directory in sources: for artifact in os.listdir(directory): if artifact in illegal: rmtree(directory + '/' + artifact) except OSError: logger.exception( '{}: Illegal file object detected, but unable to remove {}'.format( module, archive)) return False return True