Skip to content

Commit 913a94a

Browse files
author
Peter Mounce
committed
Apply automatic fixes for python
1 parent f29698c commit 913a94a

File tree

6 files changed

+277
-133
lines changed

6 files changed

+277
-133
lines changed

examples/buildkite-trigger.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,38 @@
33
# my-pipeline change-commit //depot/... "python %//depot/scripts/buildkite-trigger.py% <pipeline> %changelist% %user%"
44
import sys
55
import subprocess
6+
67
try:
78
from urllib.request import urlopen, Request
89
except ImportError:
910
from urllib2 import urlopen, Request
1011
import json
1112

1213
__BUILDKITE_TOKEN__ = "<your_token>"
13-
14+
1415
__ORG_SLUG__ = "<your_org>"
1516
pipeline_slug = sys.argv[1]
1617
changelist = sys.argv[2]
1718
user = sys.argv[3]
1819

19-
description = subprocess.check_output(["p4", "-Ztag", "-F", "%desc%", "describe", changelist])
20+
description = subprocess.check_output(
21+
["p4", "-Ztag", "-F", "%desc%", "describe", changelist]
22+
)
2023

2124
headers = {
2225
'Content-Type': 'application/json',
23-
'Authorization': 'Bearer %s' % __BUILDKITE_TOKEN__
26+
'Authorization': 'Bearer %s' % __BUILDKITE_TOKEN__,
2427
}
2528
payload = {
2629
'commit': '@' + changelist,
2730
'branch': 'master',
2831
'message': description,
29-
'author': {
30-
'name': user
31-
}
32+
'author': {'name': user},
3233
}
33-
url = "https://api.buildkite.com/v2/organizations/%s/pipelines/%s/builds" % (__ORG_SLUG__, pipeline_slug)
34+
url = "https://api.buildkite.com/v2/organizations/%s/pipelines/%s/builds" % (
35+
__ORG_SLUG__,
36+
pipeline_slug,
37+
)
3438

3539
params = json.dumps(payload).encode('utf8')
3640
req = Request(url, data=params, headers=headers)

examples/cleanup-unused-workspaces.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,24 @@
2727

2828
# Filter by basic prefix matching.
2929
# May want to include filtering by user and other fields to avoid false positives.
30-
bk_clients = [client for client in clients
31-
if client.get('client', '').startswith('bk-p4-')]
30+
bk_clients = [
31+
client for client in clients if client.get('client', '').startswith('bk-p4-')
32+
]
3233

3334
now = datetime.now()
3435
n_days_ago = (now - timedelta(days=__days_unused__)).timestamp()
35-
unused_clients = [client for client in bk_clients
36-
if int(client.get('Access')) < n_days_ago]
36+
unused_clients = [
37+
client for client in bk_clients if int(client.get('Access')) < n_days_ago
38+
]
3739

3840
pprint(unused_clients)
39-
proceed = input("Will delete %d/%d Buildkite clients. Continue? (y/n) " % (len(unused_clients),len(bk_clients))).lower() == 'y'
41+
proceed = (
42+
input(
43+
"Will delete %d/%d Buildkite clients. Continue? (y/n) "
44+
% (len(unused_clients), len(bk_clients))
45+
).lower()
46+
== 'y'
47+
)
4048

4149
if proceed:
4250
for client in unused_clients:

python/buildkite.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@
1212
__LOCAL_RUN__ = os.environ['BUILDKITE_AGENT_NAME'] == 'local'
1313

1414
__REVISION_METADATA__ = 'buildkite-perforce-revision'
15-
__REVISION_METADATA_DEPRECATED__ = 'buildkite:perforce:revision' # old metadata key, incompatible with `bk local run`
15+
__REVISION_METADATA_DEPRECATED__ = (
16+
'buildkite:perforce:revision' # old metadata key, incompatible with `bk local run`
17+
)
18+
1619

1720
def get_env():
1821
"""Get env vars passed in via plugin config"""
19-
env = {
20-
'P4PORT': os.environ.get('P4PORT') or os.environ.get('BUILDKITE_REPO')
21-
}
22+
env = {'P4PORT': os.environ.get('P4PORT') or os.environ.get('BUILDKITE_REPO')}
2223
for p4var in ['P4PORT', 'P4USER', 'P4TICKETS', 'P4TRUST']:
2324
plugin_value = os.environ.get('BUILDKITE_PLUGIN_PERFORCE_%s' % p4var)
2425
if plugin_value:
2526
env[p4var] = plugin_value
2627
return env
2728

29+
2830
def list_from_env_array(var):
2931
"""Read list of values from either VAR or VAR_0, VAR_1 etc"""
3032
result = os.environ.get(var, [])
3133
if result:
32-
return [result] # convert single value to list
34+
return [result] # convert single value to list
3335

3436
i = 0
3537
while True:
@@ -41,6 +43,7 @@ def list_from_env_array(var):
4143

4244
return result
4345

46+
4447
def get_config():
4548
"""Get configuration which will be passed directly to perforce.P4Repo as kwargs"""
4649
conf = {}
@@ -54,7 +57,9 @@ def get_config():
5457

5558
if 'BUILDKITE_PLUGIN_PERFORCE_ROOT' in os.environ and not __LOCAL_RUN__:
5659
raise Exception("Custom P4 root is for use in unit tests only")
57-
conf['root'] = os.environ.get('BUILDKITE_PLUGIN_PERFORCE_ROOT') or os.environ.get('BUILDKITE_BUILD_CHECKOUT_PATH')
60+
conf['root'] = os.environ.get('BUILDKITE_PLUGIN_PERFORCE_ROOT') or os.environ.get(
61+
'BUILDKITE_BUILD_CHECKOUT_PATH'
62+
)
5863

5964
# Coerce view into pairs of [depot client] paths
6065
view_parts = conf['view'].split(' ')
@@ -63,27 +68,35 @@ def get_config():
6368
conf['view'] = ['%s %s' % (v, next(view_iter)) for v in view_iter]
6469
return conf
6570

71+
6672
def get_metadata(key):
6773
"""If it exists, retrieve metadata from buildkite for a given key"""
6874
if not __ACCESS_TOKEN__:
6975
# Cannot get metadata outside of buildkite context
7076
return None
7177

7278
if subprocess.call(['buildkite-agent', 'meta-data', 'exists', key]) == 0:
73-
return subprocess.check_output(['buildkite-agent', 'meta-data', 'get', key]).decode(sys.stdout.encoding)
79+
return subprocess.check_output(
80+
['buildkite-agent', 'meta-data', 'get', key]
81+
).decode(sys.stdout.encoding)
82+
7483

7584
def set_metadata(key, value, overwrite=False):
76-
""" Set metadata in buildkite for a given key. Optionally overwrite existing data.
77-
Returns true if data was written
85+
"""Set metadata in buildkite for a given key. Optionally overwrite existing data.
86+
Returns true if data was written
7887
"""
7988
if not __ACCESS_TOKEN__ or __LOCAL_RUN__:
8089
# Cannot set metadata outside of buildkite context, including `bk local run`
8190
return False
8291

83-
if overwrite or subprocess.call(['buildkite-agent', 'meta-data', 'exists', key]) == 100:
84-
subprocess.call(['buildkite-agent', 'meta-data', 'set', key, value])
92+
if (
93+
overwrite
94+
or subprocess.call(['buildkite-agent', 'meta-data', 'exists', key]) == 100
95+
):
96+
subprocess.call(['buildkite-agent', 'meta-data', 'set', key, value])
8597
return True
8698

99+
87100
def get_users_changelist():
88101
"""Get the shelved changelist supplied by the user, if applicable"""
89102
# Overrides the CL to unshelve via plugin config
@@ -96,11 +109,14 @@ def get_users_changelist():
96109
if branch.isdigit():
97110
return branch
98111

112+
99113
def get_build_revision():
100114
"""Get a p4 revision for the build from buildkite context"""
101-
revision = get_metadata(__REVISION_METADATA__) or \
102-
get_metadata(__REVISION_METADATA_DEPRECATED__) or \
103-
os.environ['BUILDKITE_COMMIT'] # HEAD, user-defined revision or git-sha
115+
revision = (
116+
get_metadata(__REVISION_METADATA__)
117+
or get_metadata(__REVISION_METADATA_DEPRECATED__)
118+
or os.environ['BUILDKITE_COMMIT']
119+
) # HEAD, user-defined revision or git-sha
104120

105121
# Convert bare changelist number to revision specifier
106122
# Note: Theoretically, its possible for all 40 characters of a git sha to be digits.
@@ -113,12 +129,16 @@ def get_build_revision():
113129
# Unable to establish a concrete revision for the build
114130
return None
115131

132+
116133
def set_build_revision(revision):
117134
"""Set the p4 revision for following jobs in this build"""
118135
set_metadata(__REVISION_METADATA__, revision)
119136
set_metadata(__REVISION_METADATA_DEPRECATED__, revision)
120137

138+
121139
def set_build_info(revision, description):
122140
"""Set the description and commit number in the UI for this build by mimicking a git repo"""
123-
revision = revision.lstrip('@#') # revision must look like a git sha for buildkite to accept it
141+
revision = revision.lstrip(
142+
'@#'
143+
) # revision must look like a git sha for buildkite to accept it
124144
set_metadata('buildkite:git:commit', 'commit %s\n\n\t%s' % (revision, description))

python/checkout.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
import subprocess
77

88
from perforce import P4Repo
9-
from buildkite import (get_env, get_config, get_build_revision, set_build_revision,
10-
get_users_changelist, set_build_info)
9+
from buildkite import (
10+
get_env,
11+
get_config,
12+
get_build_revision,
13+
set_build_revision,
14+
get_users_changelist,
15+
set_build_info,
16+
)
17+
1118

1219
def main():
1320
"""Main"""
@@ -29,7 +36,8 @@ def main():
2936

3037
description = repo.description(
3138
# Prefer users change description over latest submitted change
32-
user_changelist or repo.head_at_revision(revision)
39+
user_changelist
40+
or repo.head_at_revision(revision)
3341
)
3442
set_build_info(revision, description)
3543

0 commit comments

Comments
 (0)