Skip to content

Commit

Permalink
updated v1.2.0
Browse files Browse the repository at this point in the history
- removed deprecated pkg_resources module
- added better version checker
- upgraded app2script module to handle conversions
- added a projects tool to fetch projects with earthengine api enabled
- upgraded quota tool to handle root assets for earthengine-api >=v0.1.400
  • Loading branch information
samapriya committed Jun 5, 2024
1 parent 8b4c04a commit 0015e4d
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 59 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

#### v1.2.0
- removed deprecated pkg_resources module
- added better version checker
- upgraded app2script module to handle conversions
- added a projects tool to fetch projects with earthengine api enabled
- upgraded quota tool to handle root assets for earthengine-api >=v0.1.400

#### v1.1.0
- added recursive handling for folder object count
- fixed [Issue 18](https://github.com/samapriya/gee_asset_manager_addon/issues/18) to handle featureview operations
Expand Down
32 changes: 32 additions & 0 deletions docs/projects/projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# EE API Enabled Projects tool

The projects tool is a relatively new add and allows the owners of cloud projects to understand which cloud projects have earth engine api enabled. The tool only works on cloud projects you own because it needs to query all permissions to check for this. These are also the projects you should be able to add to your earth engine code editor.

#### Prerequisites
* This tool assumes you have gcloud installed and authenticated. It does check for these two things as it tries to run the tool.
* This assumes that the user has enabled the earth engine api on these projects since this is what the tool is checking for.

#### Features
- **Automatically checks gcloud installation and config** this is handy because it allows the user to know if they check their base configuration
- **Prints Project Name and Number** this is useful as a list to know which projects to set if needed for your python projects as well or to find out which projects to use in code editor.

#### Sample run

```
geeadd projects
```

#### Sample output

```
gcloud is installed and authenticated
Checking Earth Engine permissions for all projects...
Project Name: abc Project Number: 77544
Project Name: def Project Number: 433
Project Name: ghi Project Number: 107921
Project Name: ijk Project Number: 225
```
2 changes: 1 addition & 1 deletion geeadd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__author__ = "Samapriya Roy"
__email__ = "[email protected]"
__version__ = "1.1.0"
__version__ = "1.2.0"
2 changes: 1 addition & 1 deletion geeadd/acl_changer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__copyright__ = """
Copyright 2021 Samapriya Roy
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
49 changes: 18 additions & 31 deletions geeadd/app2script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__copyright__ = """
Copyright 2020 Samapriya Roy
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
"""
__license__ = "Apache 2.0"
import requests
from bs4 import BeautifulSoup
import jsbeautifier


def jsext(url, outfile):
Expand All @@ -29,36 +29,23 @@ def jsext(url, outfile):
outfile (str, optional): Output file path for saving the JavaScript code.
"""
source = requests.get(url)
html_content = source.text
soup = BeautifulSoup(html_content, "html.parser")
head, tail = url.split("/view/")
fetch_url = f"{head}/javascript/{tail}-modules.json"
source_fetch = requests.get(
fetch_url,
)
if source_fetch.status_code == 200:
js_code = source_fetch.json()["dependencies"]
script_path = source_fetch.json()["path"]
js_code = js_code[script_path]

formatted_code = jsbeautifier.beautify(js_code)
try:
for articles in soup.find_all("script"):
if not articles.string == None and articles.string.strip().startswith(
"init"
):
url = articles.string.strip().split('"')[1]
if url.startswith("https"):
iscript = requests.get(url).json()
pt = iscript["path"]
if outfile == None:
print("\n")
print(
iscript["dependencies"][pt]
.encode("utf-8")
.decode("utf-8")
.strip()
)
else:
file = open(outfile, "w", encoding="utf-8")
file.write(str(iscript["dependencies"][pt]).strip())
file.close()
clean_lines = []
with open(outfile, "r", encoding="utf-8") as f:
lines = f.readlines()
clean_lines = [l.strip("\n") for l in lines if l.strip()]
with open(outfile, "w", encoding="utf-8") as f:
f.writelines("\n".join(clean_lines))
if outfile == None:
print(formatted_code)
else:
with open(outfile, 'w') as f:
f.write(formatted_code)
except Exception as e:
print(e)

Expand Down
2 changes: 1 addition & 1 deletion geeadd/batch_copy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__copyright__ = """
Copyright 2021 Samapriya Roy
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion geeadd/batch_mover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__copyright__ = """
Copyright 2021 Samapriya Roy
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions geeadd/ee_del_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__copyright__ = """
Copyright 2019 Samapriya Roy
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,9 +18,10 @@
"""
__license__ = "Apache 2.0"
import ee
import os

import ee


def delprop(collection_path, property):
ee.Initialize()
Expand Down
108 changes: 108 additions & 0 deletions geeadd/ee_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
__copyright__ = """
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import json
import shlex
import subprocess
import sys


def is_gcloud_installed():
try:
# Run the 'gcloud --version' command to check if gcloud is installed
result = subprocess.run(['gcloud', '--version'], shell=True,capture_output=True, text=True)
if result.returncode == 0:
return True
else:
return False
except FileNotFoundError:
return False

def is_gcloud_authenticated():
try:
# Run the 'gcloud auth list' command to check if gcloud is authenticated
result = subprocess.run(['gcloud', 'auth', 'list'], shell=True,capture_output=True, text=True)
if result.returncode == 0:
output = result.stdout.strip()
if "ACTIVE" in output:
return True
else:
return False
else:
return False
except FileNotFoundError:
return False

def check_gcloud():
if is_gcloud_installed() and is_gcloud_authenticated():
return True
else:
return False

def list_enabled_services(pname):
"""Lists enabled services for a given project.
Args:
pname: The project name.
Returns:
A list of enabled services as a JSON object, or None if an error occurs.
"""
command = f"gcloud services list --project={pname} --enabled --format=json"
try:
output = subprocess.run(shlex.split(command), shell=True, capture_output=True, text=True)
return json.loads(output.stdout)
except subprocess.CalledProcessError as e:
print(f"Error: gcloud command failed with code {e.returncode}.")
return None
except Exception as e:
return None
def project_permissions(pname):
"""Checks if Earth Engine API is enabled for a project.
Args:
pname: The project name.
"""
enabled_services = list_enabled_services(pname)
data = enabled_services
enabled_api_list = []
if data is not None:
for item in data:
project_id = item["parent"]
enabled_api_list.append(item['name'].split('services/')[-1])
enabled_api_list = [item['name'].split('services/')[-1] for item in data if data is not None]
if "earthengine.googleapis.com" in enabled_api_list:
print(f"Project Name: {pname} Project Number: {project_id.split('/')[-1]}")

def get_projects():
"""Retrieves project list and checks Earth Engine permissions."""
if not check_gcloud():
sys.exit("gcloud is either not installed or not authenticated.")
else:
print("\n"+"gcloud is installed and authenticated")
command = f"gcloud projects list --format=json"
try:
print("\n"+"Checking Earth Engine permissions for all projects..."+"\n")
services_json = subprocess.check_output(command, shell=True, text=True)
project_json = json.loads(services_json)
for item in project_json:
project_permissions(item['projectId'])
except subprocess.CalledProcessError as e:
print("Error:", e.output)

#get_projects()
2 changes: 1 addition & 1 deletion geeadd/ee_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__copyright__ = """
Copyright 2020 Samapriya Roy
Copyright 2024 Samapriya Roy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 0015e4d

Please sign in to comment.