Skip to content

Instantly share code, notes, and snippets.

@anviar
Created May 4, 2018 13:02
Show Gist options
  • Save anviar/03b74c3b676f77c21ab929cfc728ee9e to your computer and use it in GitHub Desktop.
Save anviar/03b74c3b676f77c21ab929cfc728ee9e to your computer and use it in GitHub Desktop.
Script to publish things on various platforms
#!/usr/bin/env python3
import os
import yaml
import argparse
import subprocess
import logging
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open('build.yml', 'r') as config_obj:
config = yaml.load(config_obj)
argparser = argparse.ArgumentParser(description='Publicate builds')
argparser.add_argument('--verbose', help='verbose level, default "info"', type=str,
choices=['debug', 'info', 'warning', 'error'], default='info')
argparser.add_argument('-b', '--build', help='process only specified build', type=str)
args = argparser.parse_args()
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=args.verbose.upper())
def publish_google(build_name):
logging.info('Processing ' + build_name)
logging.debug('Type: android')
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
authorized_session = ServiceAccountCredentials.from_json_keyfile_name(
config['builds'][build_name]['android']['credentials_file'],
scopes=['https://www.googleapis.com/auth/androidpublisher']).authorize(Http())
apk_path = os.path.join(
config['builds'][build_name]['path'],
config['builds'][build_name]['environment']['app_name'] + '.apk')
service = build('androidpublisher', 'v2', http=authorized_session)
package_name = config['builds'][build_name]['android']['package_name']
edit_request = service.edits().insert(body={}, packageName=package_name)
edit_id = edit_request.execute()['id']
apk_response = service.edits().apks().upload(
editId=edit_id,
packageName=package_name,
media_mime_type='application/octet-stream',
media_body=apk_path).execute()
logging.info('Version code %d has been uploaded' % apk_response['versionCode'])
track_response = service.edits().tracks().update(
editId=edit_id,
track=config['builds'][build_name]['android']['track'],
packageName=package_name,
body={u'versionCodes': [apk_response['versionCode']]}).execute()
logging.info('Track %s is set for version code(s) %s' %
(track_response['track'], str(track_response['versionCodes'])))
commit_request = service.edits().commit(editId=edit_id, packageName=package_name).execute()
logging.info('Edit "%s" has been committed' % (commit_request['id']))
def publish_external(build_type, build_name):
logging.info('Processing ' + build_name)
logging.debug('Type: ' + build_type)
runtime_env = {
**os.environ,
'dnp_workdir': config['builds'][build_name]['path'],
'dnp_verbose': args.verbose
}
if 'environment' in config['builds'][build_name]:
for env_variable, env_value in config['builds'][build_name]['environment'].items():
runtime_env.update({env_variable: env_value})
scenario = subprocess.Popen(
config['builds'][build_name][build_type],
env=runtime_env,
cwd=config['builds'][build_name]['path']
)
scenario.wait()
if scenario.returncode != 0:
logging.error("Returned %i for %s" %
(scenario.returncode, build_name))
return(scenario.returncode)
def publish_google_storage(build_name):
from google.cloud.storage import Client as GoogleStorageClient
from google.oauth2 import service_account
client = GoogleStorageClient(
project=config['builds'][build_name]['google-storage']['project'],
credentials=service_account.Credentials.from_service_account_file(
config['builds'][build_name]['google-storage']['credentials_file']
)
)
bucket = client.get_bucket(config['builds'][build_name]['google-storage']['bucket'])
tasks = [[
config['builds'][build_name]['google-storage']['local_path'],
config['builds'][build_name]['google-storage']['remote_path']]]
if config['builds'][build_name]['google-storage']['remote_path'].endswith('.dmg'):
tasks.append([
os.path.join(config['builds'][build_name]['path'],
config['builds'][build_name]['file_name']),
config['builds'][build_name]['google-storage']['remote_path'].replace('.dmg', '.zip')])
for local_path, remote_path in tasks:
blob = bucket.blob(remote_path)
with open(local_path, 'rb') as local_file:
blob.upload_from_file(local_file)
# print('{} -> {}'.format(local_path, remote_path))
print('URL: https://storage.googleapis.com/'
+ config['builds'][build_name]['google-storage']['bucket'] + '/' + remote_path)
def publish_ssh(build_name):
import paramiko
ssh_transport = paramiko.Transport((
config['builds'][build_name]['ssh']['host'],
config['builds'][build_name]['ssh']['port']))
tasks = [[
config['builds'][build_name]['ssh']['local_path'],
config['builds'][build_name]['ssh']['remote_path']]]
if config['builds'][build_name]['ssh']['remote_path'].endswith('.dmg'):
tasks.append([
os.path.join(config['builds'][build_name]['path'],
config['builds'][build_name]['file_name']),
config['builds'][build_name]['ssh']['remote_path'].replace('.dmg', '.zip')])
pkey = paramiko.RSAKey.from_private_key_file(os.path.join(os.getenv('HOME'), '.ssh', 'id_rsa'))
ssh_transport.connect(username=config['builds'][build_name]['ssh']['username'],
pkey=pkey)
sftp = paramiko.SFTPClient.from_transport(ssh_transport)
for local_path, remote_path in tasks:
sftp.put(local_path, remote_path)
print('URL: '
+ config['builds'][build_name]['ssh']['url']
+ os.path.basename(remote_path))
sftp.close()
ssh_transport.close()
def auto_process_by_type(build_name):
if 'android' in config['builds'][build_name]:
publish_google(build_name)
elif 'ios' in config['builds'][build_name]:
publish_external('ios', build_name)
elif 'mac' in config['builds'][build_name]:
publish_external('mac', build_name)
elif 'ssh' in config['builds'][build_name]:
publish_ssh(build_name)
elif 'google-storage' in config['builds'][build_name]:
publish_google_storage(build_name)
else:
logging.debug('Skipped unknow type ' + build_name)
if args.build is None:
for build in config['builds']:
auto_process_by_type(build)
else:
if args.build in config['builds']:
auto_process_by_type(args.build)
else:
logging.error('Unknown build ' + args.build)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment