Spaces:
Sleeping
Sleeping
HaochenGong commited on
Commit ·
976e2f2
1
Parent(s): f75ae3f
google drive
Browse files
.idea/workspace.xml
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
</component>
|
| 6 |
<component name="ChangeListManager">
|
| 7 |
<list default="true" id="5e4481c0-7ba2-42e4-bbe6-4c36a0d36baa" name="Changes" comment="debug">
|
| 8 |
-
<change afterPath="$PROJECT_DIR$/
|
| 9 |
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
| 10 |
<change beforePath="$PROJECT_DIR$/app.py" beforeDir="false" afterPath="$PROJECT_DIR$/app.py" afterDir="false" />
|
| 11 |
</list>
|
|
@@ -44,6 +44,12 @@
|
|
| 44 |
"vue.rearranger.settings.migration": "true"
|
| 45 |
}
|
| 46 |
}</component>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
| 48 |
<component name="TaskManager">
|
| 49 |
<task active="true" id="Default" summary="Default task">
|
|
@@ -54,7 +60,7 @@
|
|
| 54 |
<updated>1723386888312</updated>
|
| 55 |
<workItem from="1723386893769" duration="12000" />
|
| 56 |
<workItem from="1723386970626" duration="451000" />
|
| 57 |
-
<workItem from="1723387453009" duration="
|
| 58 |
</task>
|
| 59 |
<task id="LOCAL-00001" summary="create">
|
| 60 |
<created>1723387548126</created>
|
|
@@ -77,7 +83,42 @@
|
|
| 77 |
<option name="project" value="LOCAL" />
|
| 78 |
<updated>1723388638252</updated>
|
| 79 |
</task>
|
| 80 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
<servers />
|
| 82 |
</component>
|
| 83 |
<component name="TypeScriptGeneratedFilesManager">
|
|
@@ -86,6 +127,8 @@
|
|
| 86 |
<component name="VcsManagerConfiguration">
|
| 87 |
<MESSAGE value="create" />
|
| 88 |
<MESSAGE value="add log dir" />
|
|
|
|
|
|
|
| 89 |
<MESSAGE value="debug" />
|
| 90 |
<option name="LAST_COMMIT_MESSAGE" value="debug" />
|
| 91 |
</component>
|
|
|
|
| 5 |
</component>
|
| 6 |
<component name="ChangeListManager">
|
| 7 |
<list default="true" id="5e4481c0-7ba2-42e4-bbe6-4c36a0d36baa" name="Changes" comment="debug">
|
| 8 |
+
<change afterPath="$PROJECT_DIR$/google_drive_credentials/client_secret_372757176543-9jt0fquta6mke8v1rnbc2i766ed8gks0.apps.googleusercontent.com.json" afterDir="false" />
|
| 9 |
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
| 10 |
<change beforePath="$PROJECT_DIR$/app.py" beforeDir="false" afterPath="$PROJECT_DIR$/app.py" afterDir="false" />
|
| 11 |
</list>
|
|
|
|
| 44 |
"vue.rearranger.settings.migration": "true"
|
| 45 |
}
|
| 46 |
}</component>
|
| 47 |
+
<component name="RecentsManager">
|
| 48 |
+
<key name="MoveFile.RECENT_KEYS">
|
| 49 |
+
<recent name="$PROJECT_DIR$/google_drive_credentials" />
|
| 50 |
+
<recent name="$PROJECT_DIR$" />
|
| 51 |
+
</key>
|
| 52 |
+
</component>
|
| 53 |
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
| 54 |
<component name="TaskManager">
|
| 55 |
<task active="true" id="Default" summary="Default task">
|
|
|
|
| 60 |
<updated>1723386888312</updated>
|
| 61 |
<workItem from="1723386893769" duration="12000" />
|
| 62 |
<workItem from="1723386970626" duration="451000" />
|
| 63 |
+
<workItem from="1723387453009" duration="7509000" />
|
| 64 |
</task>
|
| 65 |
<task id="LOCAL-00001" summary="create">
|
| 66 |
<created>1723387548126</created>
|
|
|
|
| 83 |
<option name="project" value="LOCAL" />
|
| 84 |
<updated>1723388638252</updated>
|
| 85 |
</task>
|
| 86 |
+
<task id="LOCAL-00004" summary="logs">
|
| 87 |
+
<created>1723389453664</created>
|
| 88 |
+
<option name="number" value="00004" />
|
| 89 |
+
<option name="presentableId" value="LOCAL-00004" />
|
| 90 |
+
<option name="project" value="LOCAL" />
|
| 91 |
+
<updated>1723389453664</updated>
|
| 92 |
+
</task>
|
| 93 |
+
<task id="LOCAL-00005" summary="logs">
|
| 94 |
+
<created>1723389809864</created>
|
| 95 |
+
<option name="number" value="00005" />
|
| 96 |
+
<option name="presentableId" value="LOCAL-00005" />
|
| 97 |
+
<option name="project" value="LOCAL" />
|
| 98 |
+
<updated>1723389809864</updated>
|
| 99 |
+
</task>
|
| 100 |
+
<task id="LOCAL-00006" summary="dubug">
|
| 101 |
+
<created>1723390353091</created>
|
| 102 |
+
<option name="number" value="00006" />
|
| 103 |
+
<option name="presentableId" value="LOCAL-00006" />
|
| 104 |
+
<option name="project" value="LOCAL" />
|
| 105 |
+
<updated>1723390353091</updated>
|
| 106 |
+
</task>
|
| 107 |
+
<task id="LOCAL-00007" summary="debug">
|
| 108 |
+
<created>1723390589691</created>
|
| 109 |
+
<option name="number" value="00007" />
|
| 110 |
+
<option name="presentableId" value="LOCAL-00007" />
|
| 111 |
+
<option name="project" value="LOCAL" />
|
| 112 |
+
<updated>1723390589691</updated>
|
| 113 |
+
</task>
|
| 114 |
+
<task id="LOCAL-00008" summary="debug">
|
| 115 |
+
<created>1723390945723</created>
|
| 116 |
+
<option name="number" value="00008" />
|
| 117 |
+
<option name="presentableId" value="LOCAL-00008" />
|
| 118 |
+
<option name="project" value="LOCAL" />
|
| 119 |
+
<updated>1723390945723</updated>
|
| 120 |
+
</task>
|
| 121 |
+
<option name="localTasksCounter" value="9" />
|
| 122 |
<servers />
|
| 123 |
</component>
|
| 124 |
<component name="TypeScriptGeneratedFilesManager">
|
|
|
|
| 127 |
<component name="VcsManagerConfiguration">
|
| 128 |
<MESSAGE value="create" />
|
| 129 |
<MESSAGE value="add log dir" />
|
| 130 |
+
<MESSAGE value="logs" />
|
| 131 |
+
<MESSAGE value="dubug" />
|
| 132 |
<MESSAGE value="debug" />
|
| 133 |
<option name="LAST_COMMIT_MESSAGE" value="debug" />
|
| 134 |
</component>
|
app.py
CHANGED
|
@@ -16,30 +16,43 @@ import psutil
|
|
| 16 |
from SEM.run_single_sem import run_single_pp
|
| 17 |
from CDM.run_single import run_single_img
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
title = "Cpp4App_test"
|
| 20 |
description = "Automated Contextual Privacy Policies Generation for Mobile Apps"
|
| 21 |
|
| 22 |
|
| 23 |
# log
|
| 24 |
-
log_file_path = './app.log'
|
| 25 |
-
log_directory = os.path.dirname(log_file_path)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
try:
|
| 31 |
-
with open(log_file_path, 'w') as f:
|
| 32 |
-
f.write('Test log entry')
|
| 33 |
-
f.flush()
|
| 34 |
-
os.fsync(f.fileno())
|
| 35 |
-
print(f"Successfully wrote to {log_file_path}")
|
| 36 |
-
except Exception as e:
|
| 37 |
-
print(f"Failed to write to {log_file_path}: {e}")
|
| 38 |
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# set log handler(Generate one log file per day and keep only the latest 7 files)
|
| 42 |
-
handler = TimedRotatingFileHandler(log_file_path, when='
|
| 43 |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
| 44 |
handler.setLevel(logging.INFO)
|
| 45 |
handler.setFormatter(formatter)
|
|
@@ -52,6 +65,46 @@ logger.addHandler(handler)
|
|
| 52 |
logger.info('Application started')
|
| 53 |
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def write_and_read():
|
| 56 |
# Write
|
| 57 |
with open('myfile.txt', 'w') as f:
|
|
@@ -236,11 +289,15 @@ def schedule_restarts(interval_hours):
|
|
| 236 |
python = sys.executable
|
| 237 |
os.execl(python, python, *sys.argv)
|
| 238 |
|
| 239 |
-
def
|
| 240 |
"""
|
| 241 |
Logging system resource usage every 'interval_hours' hours
|
| 242 |
"""
|
|
|
|
| 243 |
while True:
|
|
|
|
|
|
|
|
|
|
| 244 |
cpu_usage = psutil.cpu_percent(interval=1)
|
| 245 |
memory_info = psutil.virtual_memory()
|
| 246 |
disk_usage = psutil.disk_usage('/')
|
|
@@ -251,7 +308,19 @@ def schedule_daily_resource_logging(interval_hours):
|
|
| 251 |
logger.info(
|
| 252 |
f"Disk usage: {disk_usage.percent}% (Total: {disk_usage.total}, Used: {disk_usage.used}, Free: {disk_usage.free})")
|
| 253 |
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
|
| 257 |
demo = gr.Blocks()
|
|
@@ -349,7 +418,7 @@ with demo:
|
|
| 349 |
# gr.Html(style=hide_style).show()
|
| 350 |
|
| 351 |
threading.Thread(target=schedule_restarts, args=(72,)).start() # restart every 72 hours
|
| 352 |
-
threading.Thread(target=
|
| 353 |
|
| 354 |
try:
|
| 355 |
demo.launch()
|
|
|
|
| 16 |
from SEM.run_single_sem import run_single_pp
|
| 17 |
from CDM.run_single import run_single_img
|
| 18 |
|
| 19 |
+
from google.oauth2 import service_account
|
| 20 |
+
from googleapiclient.discovery import build
|
| 21 |
+
from googleapiclient.http import MediaFileUpload
|
| 22 |
+
|
| 23 |
title = "Cpp4App_test"
|
| 24 |
description = "Automated Contextual Privacy Policies Generation for Mobile Apps"
|
| 25 |
|
| 26 |
|
| 27 |
# log
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# 认证并创建Google Drive服务
|
| 30 |
+
def authenticate_gdrive():
|
| 31 |
+
SCOPES = ['https://www.googleapis.com/auth/drive.file']
|
| 32 |
+
creds = service_account.Credentials.from_service_account_file('google_drive_credentials/client_secret_372757176543-9jt0fquta6mke8v1rnbc2i766ed8gks0.apps.googleusercontent.com.json', scopes=SCOPES)
|
| 33 |
+
service = build('drive', 'v3', credentials=creds)
|
| 34 |
+
return service
|
| 35 |
+
|
| 36 |
+
# 上传文件到Google Drive
|
| 37 |
+
def upload_to_gdrive(file_path, service):
|
| 38 |
+
file_metadata = {
|
| 39 |
+
'name': os.path.basename(file_path),
|
| 40 |
+
'parents': ['1Ukx6pQcbUQssV7tyWLGZE0QAzmSjBKUK'] # Google Drive上的文件夹ID
|
| 41 |
+
}
|
| 42 |
+
media = MediaFileUpload(file_path, resumable=True)
|
| 43 |
+
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 44 |
+
print(f"File ID: {file.get('id')}")
|
| 45 |
+
return file.get('id')
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
log_directory = './logs'
|
| 49 |
+
log_file_path = os.path.join(log_directory, 'app.log')
|
| 50 |
|
| 51 |
+
if not os.path.exists(log_directory):
|
| 52 |
+
os.makedirs(log_directory)
|
| 53 |
|
| 54 |
# set log handler(Generate one log file per day and keep only the latest 7 files)
|
| 55 |
+
handler = TimedRotatingFileHandler(log_file_path, when='midnight', interval=1, backupCount=7, utc=True)
|
| 56 |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
| 57 |
handler.setLevel(logging.INFO)
|
| 58 |
handler.setFormatter(formatter)
|
|
|
|
| 65 |
logger.info('Application started')
|
| 66 |
|
| 67 |
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def create_and_upload_test_logs():
|
| 73 |
+
"""
|
| 74 |
+
生成两个测试日志文件,并验证排序与上传功能
|
| 75 |
+
"""
|
| 76 |
+
service = authenticate_gdrive()
|
| 77 |
+
|
| 78 |
+
# 生成第一个测试日志文件
|
| 79 |
+
logger.info("This is the first test log entry.")
|
| 80 |
+
handler.doRollover() # 触发日志文件轮替
|
| 81 |
+
|
| 82 |
+
# 生成第二个测试日志文件
|
| 83 |
+
logger.info("This is the second test log entry.")
|
| 84 |
+
handler.doRollover() # 触发日志文件轮替
|
| 85 |
+
|
| 86 |
+
# 获取日志目录中的所有文件,并根据修改时间排序
|
| 87 |
+
current_log_files = sorted(
|
| 88 |
+
[f for f in os.listdir(log_directory) if f.startswith('app.log')],
|
| 89 |
+
key=lambda x: os.path.getmtime(os.path.join(log_directory, x)),
|
| 90 |
+
reverse=True
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if current_log_files:
|
| 94 |
+
# 找到排序后的第一个文件,即最新生成的日志文件
|
| 95 |
+
latest_file_name = os.path.join(log_directory, current_log_files[0])
|
| 96 |
+
if os.path.exists(latest_file_name):
|
| 97 |
+
upload_to_gdrive(latest_file_name, service)
|
| 98 |
+
print(f"Latest log file '{latest_file_name}' uploaded successfully.")
|
| 99 |
+
|
| 100 |
+
# 调用测试函数
|
| 101 |
+
create_and_upload_test_logs()
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
def write_and_read():
|
| 109 |
# Write
|
| 110 |
with open('myfile.txt', 'w') as f:
|
|
|
|
| 289 |
python = sys.executable
|
| 290 |
os.execl(python, python, *sys.argv)
|
| 291 |
|
| 292 |
+
def schedule_log_upload(interval_hours):
|
| 293 |
"""
|
| 294 |
Logging system resource usage every 'interval_hours' hours
|
| 295 |
"""
|
| 296 |
+
service = authenticate_gdrive()
|
| 297 |
while True:
|
| 298 |
+
time.sleep(interval_hours * 3600) # 等待指定小时数
|
| 299 |
+
|
| 300 |
+
# 记录系统资源使用情况
|
| 301 |
cpu_usage = psutil.cpu_percent(interval=1)
|
| 302 |
memory_info = psutil.virtual_memory()
|
| 303 |
disk_usage = psutil.disk_usage('/')
|
|
|
|
| 308 |
logger.info(
|
| 309 |
f"Disk usage: {disk_usage.percent}% (Total: {disk_usage.total}, Used: {disk_usage.used}, Free: {disk_usage.free})")
|
| 310 |
|
| 311 |
+
# 触发日志文件轮替前,记录当前文件名
|
| 312 |
+
current_log_files = sorted([f for f in os.listdir(log_directory) if f.startswith('app.log')],
|
| 313 |
+
key=lambda x: os.path.getmtime(os.path.join(log_directory, x)),
|
| 314 |
+
reverse=True)
|
| 315 |
+
if current_log_files:
|
| 316 |
+
rotated_file_name = os.path.join(log_directory, current_log_files[0])
|
| 317 |
+
|
| 318 |
+
# 触发日志文件轮替
|
| 319 |
+
handler.doRollover()
|
| 320 |
+
|
| 321 |
+
# 上传轮替后的文件到 Google Drive
|
| 322 |
+
if os.path.exists(rotated_file_name):
|
| 323 |
+
upload_to_gdrive(rotated_file_name, service)
|
| 324 |
|
| 325 |
|
| 326 |
demo = gr.Blocks()
|
|
|
|
| 418 |
# gr.Html(style=hide_style).show()
|
| 419 |
|
| 420 |
threading.Thread(target=schedule_restarts, args=(72,)).start() # restart every 72 hours
|
| 421 |
+
threading.Thread(target=schedule_log_upload, args=(24,)).start() # recorded every 24 hours
|
| 422 |
|
| 423 |
try:
|
| 424 |
demo.launch()
|
google_drive_credentials/client_secret_372757176543-9jt0fquta6mke8v1rnbc2i766ed8gks0.apps.googleusercontent.com.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"web":{"client_id":"372757176543-9jt0fquta6mke8v1rnbc2i766ed8gks0.apps.googleusercontent.com","project_id":"cpp4app-logs","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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"}}
|