Debian Codegraph @

docs/examples/timer_management/create_timer_data_access.py

raw
1 #!/usr/bin/env python
2
3 import argparse
4 import datetime
5
6 import globus_sdk
7 from globus_sdk.experimental.globus_app import UserApp
8
9 # Tutorial Client ID - <replace this with your own client>
10 NATIVE_CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2"
11 USER_APP = UserApp("manage-timers-example", client_id=NATIVE_CLIENT_ID)
12
13
14 def uses_data_access(transfer_client, collection_id):
15 doc = transfer_client.get_endpoint(collection_id)
16 if doc["entity_type"] != "GCSv5_mapped_collection":
17 return False
18 if doc["high_assurance"]:
19 return False
20 return True
21
22
23 def main():
24 parser = argparse.ArgumentParser()
25 # the source, destination, and path to a file or dir to sync
26 parser.add_argument("SOURCE_COLLECTION")
27 parser.add_argument("DESTINATION_COLLECTION")
28 parser.add_argument("PATH")
29 parser.add_argument(
30 "--interval-seconds",
31 help="How frequently the timer runs, in seconds (default: 1 hour)",
32 default=3600,
33 type=int,
34 )
35 parser.add_argument(
36 "--days",
37 help="How many days to run the timer (default: 2)",
38 default=2,
39 type=int,
40 )
41 args = parser.parse_args()
42
43 timers_client = globus_sdk.TimersClient(app=USER_APP)
44 transfer_client = globus_sdk.TransferClient(app=USER_APP)
45
46 # check if the source or destination use 'data_access' scopes
47 # if so, register these requirements with the app
48 if uses_data_access(transfer_client, args.SOURCE_COLLECTION):
49 timers_client.add_app_transfer_data_access_scope(args.SOURCE_COLLECTION)
50 if uses_data_access(transfer_client, args.DESTINATION_COLLECTION):
51 timers_client.add_app_transfer_data_access_scope(args.DESTINATION_COLLECTION)
52
53 # from this point onwards, the example is the same as the basic create_timer.py
54 # script -- we've handled the nuance of data_access
55 #
56 # when the timer submission runs, you *may* be prompted to login again, if
57 # 'data_access' requirements were detected
58
59 body = globus_sdk.TransferData(
60 source_endpoint=args.SOURCE_COLLECTION,
61 destination_endpoint=args.DESTINATION_COLLECTION,
62 )
63 body.add_item(args.PATH, args.PATH)
64
65 # the timer will run until the end date, on whatever interval was requested
66 schedule = globus_sdk.RecurringTimerSchedule(
67 interval_seconds=args.interval_seconds,
68 end={
69 "condition": "time",
70 "datetime": datetime.datetime.now() + datetime.timedelta(days=args.days),
71 },
72 )
73
74 timer = timers_client.create_timer(
75 timer=globus_sdk.TransferTimer(
76 name=(
77 "create-timer-example "
78 f"[created at {datetime.datetime.now().isoformat()}]"
79 ),
80 body=body,
81 schedule=schedule,
82 )
83 )
84 print("Finished submitting timer.")
85 print(f"timer_id: {timer['timer']['job_id']}")
86
87
88 if __name__ == "__main__":
89 main()